package com.myph.phctrl_t;

import com.example.phctrl_t.R;
import com.myph.model.ClientSocketNew;
import com.myph.model.Util;

import android.R.integer;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SignalActivity extends Activity implements OnClickListener {

	EditText edt_ph_low, edt_ph_high;
	Button btn_start_signal, btn_close_signal, btn_set_ph;
	String ip, port;
	StringBuilder response = new StringBuilder();
	private long prevClickTime = 0;
	int reqType;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_signal);

		ip = Util.getSharedPref(this, "ip");
		port = Util.getSharedPref(this, "port");

		edt_ph_low = (EditText) findViewById(R.id.edt_ph_low);
		edt_ph_high = (EditText) findViewById(R.id.edt_ph_high);
		btn_close_signal = (Button) findViewById(R.id.btn_close_signal);
		btn_set_ph = (Button) findViewById(R.id.btn_set_ph);
		btn_start_signal = (Button) findViewById(R.id.btn_start_signal);

//		edt_ph_low.setFocusable(false);
//		edt_ph_high.setFocusable(false);
		btn_close_signal.setOnClickListener(this);
		btn_set_ph.setOnClickListener(this);
		btn_start_signal.setOnClickListener(this);
		edt_ph_low.setOnClickListener(this);
		
		edt_ph_high.setOnClickListener(this);
	}

	@SuppressLint("HandlerLeak")
	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 1:// set ph signal
				ClientSocketNew csn = new ClientSocketNew(ip, port, handler);
				String request = "$alarmset,";
				String ph_low = "",
				ph_high = "";
				if (edt_ph_high.getText() != null) {
					ph_high = edt_ph_high.getText().toString();
				}
				if (edt_ph_low.getText() != null) {
					ph_low = edt_ph_low.getText().toString();
				}

				if (ph_low.equals("") == true || ph_high.equals("") == true) {
					Util.myToast(getApplicationContext(), "数据不能为空");
					return;
				}
				try {
					double d_high = Double.parseDouble(ph_high);
					double d_low = Double.parseDouble(ph_low);
				} catch (Exception e) {
					Util.myToast(getApplicationContext(), "数据不合法");
					return;
				}
				Double tmpDouble_low = Double.parseDouble(ph_low) * 100;
				Double tmpDouble_high = Double.parseDouble(ph_high) * 100;

				int tmp_high = tmpDouble_high.intValue();
				int tmp_low = tmpDouble_low.intValue();
				request += String.valueOf(tmp_low) + ",";
				request += String.valueOf(tmp_high) + "#";
				csn.setNewRequest(request, response);
				csn.start();
				break;
			case 2:// start signal
				String res2 = "$alarmcon,1#";
				ClientSocketNew csn2 = new ClientSocketNew(ip, port, handler);
				// csn2.setNewRequest(res2, response);
				csn2.setNewRequestForLongTime(res2, response);
				csn2.start();
				break;
			case 3:// close signal
				String res3 = "$alarmcon,0#";
				ClientSocketNew csn3 = new ClientSocketNew(ip, port, handler);
				csn3.setNewRequest(res3, response);
				csn3.start();
				break;
			case 1001:
				Toast.makeText(getApplicationContext(), "connect ok!",
						Toast.LENGTH_SHORT).show();
				// finish();
				break;
			case 1002:
				Toast.makeText(getApplicationContext(), "连接失败",
						Toast.LENGTH_SHORT).show();
				// finish();
				break;
			case 1003:
				if (response != null) {
					Log.d("DEBUG", response.toString());
					String resString = response.toString().trim();
					if (reqType == 0) {
						if (resString.contains("1")) {
							Util.myToast(getApplicationContext(), "警报设置成功~");
						}
					} else if (reqType == 1) {// 开启警报,若数据不合法会收到$danger!#

						if (resString.contains("1")) {
							Util.myToast(getApplicationContext(), "警报设置成功~");
						}
						if (resString.contains("danger")) {
							Util.myToast(getApplicationContext(),
									"pH数据超出规定范围!!!");
						}
					} else if (reqType == 2) {// 关闭警报
						if (resString.equals("1")) {
							Util.myToast(getApplicationContext(), "警报设置成功~");
						} else {
							Util.myToast(getApplicationContext(), "警报设置失败");
						}
					}
				} else {
					Util.myToast(getApplicationContext(), "异常:" + response);
				}

				// finish();
				break;
			case 1004:
				Util.myToast(getApplicationContext(), "连接超时,操作失败");
				// finish();
				break;
			case 1005:
				Util.myToast(getApplicationContext(), "结束");
				break;
			default:
				Util.myToast(getApplicationContext(), "handler error!");
				break;
			}

		}
	};

	@Override
	public void onClick(View v) {
		long pressGap = System.currentTimeMillis() - prevClickTime;
		if (pressGap < 1000) {
			return;
		} else {
			prevClickTime = System.currentTimeMillis();
		}
		switch (v.getId()) {
		case R.id.btn_set_ph:
			reqType = 0;
			Util.sendMsg(1, handler);
			break;
		case R.id.btn_start_signal:
			reqType = 1;
			Util.sendMsg(2, handler);
			break;
		case R.id.btn_close_signal:
			reqType = 2;
			Util.sendMsg(3, handler);
			break;
//		case R.id.edt_ph_high:
//			edt_ph_low.setFocusable(true);
//			edt_ph_high.setFocusable(true);
//			break;
//		case R.id.edt_ph_low:
//			edt_ph_low.setFocusable(true);
//			edt_ph_high.setFocusable(true);
//			break;
		default:
			break;
		}
	}
}
